'''
Created on Oct 12, 2013

@author: priya
'''

from xml.etree.ElementTree import Element, SubElement, tostring
from xml.dom import minidom
import re

def prettify(elem):
    """Return a pretty-printed XML string for the Element.
    """
    rough_string = tostring(elem, 'utf-8')
    reparsed = minidom.parseString(rough_string)
    return reparsed.toprettyxml(indent="  ")

docRegex = re.compile("#begin.*?#end.*?\n",re.M|re.S)
beginRegex = re.compile("#begin.*?$",re.M|re.S)
endRegex = re.compile("#end.*?$",re.M|re.S)

datasetFile = r'C:\Users\priya\Desktop\Coreference Resolution 2\SemEval2010\semeval_2010_t1_eng\data\en.train.txt'
xmlFile = r'C:\Users\priya\Desktop\Coreference Resolution 2\SemEval2010\semeval_2010_t1_eng\data\en.train.xml'
file = open(datasetFile)
fileString = file.read()
file.close()

dataset = Element('dataset')
dataset.set("name","train")

while len(fileString)>0:
    docMatchObj = docRegex.search(fileString)
    if docMatchObj is None: 
        break
    docString = docMatchObj.group() 
    fileString = fileString.replace(docString,'')
    
    document = SubElement(dataset, "document")
    document.set("id",docString[16:28])
    print docString[16:28]
    
    beginString = beginRegex.search(docString).group(0)
    endString = endRegex.search(docString).group(0)
    docstring = docString.replace(beginString,'')
    docstring = docstring.replace(endString,'')
    
    sentences = docstring.split("\n\n")
    for i in range(0,len(sentences)):
        if len(sentences[i])>1:
            sentence = SubElement(document,"sentence")
            sentence.set("id",str(i+1))
            words = sentences[i].split('\n')
            mentionStack = []
            mentionStack.append(sentence)
            neType = '-'
            inQuote = False
            for word in words:
                corefArray = []
                if len(word)>1:
                    wordarray = word.split("\t")
                    coref = wordarray[-1]
                    if len(coref)>1:
                        corefArray = coref.split('|')
                    
                    for mention in corefArray:
                        if mention.find('(')>-1:
                            menId = mention.replace('(','').replace(")",'')
                            mentionNode = SubElement(mentionStack[-1],"mention")
                            mentionNode.set("id",menId)
                            mentionStack.append(mentionNode)
                    
                    wordnode = SubElement(mentionStack[-1],"word")
                    wordnode.set("id",wordarray[0])
                    if wordarray[1] == '``':
                        inQuote = True
                    wordnode.set("inQuotes",str(inQuote))
                    if wordarray[1] == "''":
                        inQuote = False
                        
                    
                        
                    wordnode.set("token",wordarray[1])
                    wordnode.set("lemma",wordarray[2])
                    wordnode.set("plemma",wordarray[3])
                    wordnode.set("pos",wordarray[4])
                    wordnode.set("ppos",wordarray[5])
                    wordnode.set("feat",wordarray[6])
                    wordnode.set("pfeat",wordarray[7])
                    wordnode.set("head",wordarray[8])
                    wordnode.set("phead",wordarray[9])
                    wordnode.set("deprel",wordarray[10])
                    wordnode.set("pdeprel",wordarray[11])
                    curNe = wordarray[12][:]
                    if curNe.find('(')>-1:
                        neType = curNe.replace('(','').replace(')','')
                    wordnode.set("ne",neType)
                    if curNe.find(')')>-1:
                        neType = '-'
                    wordnode.set("pne",wordarray[13])
                    
                    for mention in corefArray:
                        if mention.find(')')>-1:
                            mentionStack.pop()

xmlhandle = open(xmlFile,'w')    
xmlhandle.write(prettify(dataset))
xmlhandle.close()
    
    
